//
//  FirstPageViewController.m
//  TongMiBaby
//
//  Created by 史伟文 on 15/4/2.
//  Copyright (c) 2015年 XuanRuiTechnology. All rights reserved.
//

#import "FirstPageViewController.h"
@interface FirstPageViewController ()
{
    UITableView *myTableView;
}
@property (strong, nonatomic) NSMutableArray *objects;


@end

@implementation FirstPageViewController
- (NSMutableArray *)objects {
    if (!_objects) {
        
        NSArray *arr = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_18.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_57.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_21.png"],@"活动中心",@"园所中心举办活动啦！"];
        NSArray *arr1 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_21.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_37.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_38.png"],@"中心公告",@"园所公告、通知..."];
        NSArray *arr2= @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_23.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_29.png"],@"班级花名册",@"宝宝安全保障第一位..."];
        NSArray *arr3 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_25.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_37.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_57.png"],[UIImage imageNamed:@"img_39.png"],@"家长圈子",@"班级之间互动讨论圈子！"];
        NSArray *arr4 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_27.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_37.png"],[UIImage imageNamed:@"img_38.png"],[UIImage imageNamed:@"img_21.png"],@"学生课程签到",@"2015年各班中出勤请款总概！"];
        NSArray *arr5 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_29.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_39.png"],[UIImage imageNamed:@"img_38.png"],[UIImage imageNamed:@"img_46.png"],[UIImage imageNamed:@"img_29.png"],@"我的教学计划",@"宝宝课程安排预览"];
        NSArray *arr6 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_31.png"],[UIImage imageNamed:@"img_57.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_37.png"],@"家长私信",@"宝贝骄傲的成绩体现！"];
        NSArray *arr7 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_33.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_39.png"],[UIImage imageNamed:@"img_29.png"],@"宝贝家生活",@"快快乐乐一家亲"];
        NSArray *arr8 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_35.png"],[UIImage imageNamed:@"img_45.png"],[UIImage imageNamed:@"img_37.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_38.png"],[UIImage imageNamed:@"img_46.png"],@"宝贝在校成长档案",@"宝贝在校的表现，一切尽在掌握中..."];
        NSArray *arr9 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_37.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_45.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_39.png"],[UIImage imageNamed:@"img_31.png"],@"家长推荐",@"介绍给更多好友使用。"];
        NSArray *arr10 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_39.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_57.png"],[UIImage imageNamed:@"img_37.png"],[UIImage imageNamed:@"img_23.png"],@"客户关怀",@"给您真挚的问候。"];
        NSArray *arr11 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_41.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_46.png"],[UIImage imageNamed:@"img_21.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_37.png"],@"教学百科",@"各种教学资料，供您学习。"];
        NSArray *arr12 = @[[UIImage imageNamed:@"ios_bg2_03.png"],[UIImage imageNamed:@"ios_bg_03.png"],[UIImage imageNamed:@"icon_43.png"],[UIImage imageNamed:@"img_23.png"],[UIImage imageNamed:@"img_29.png"],[UIImage imageNamed:@"img_38.png"],[UIImage imageNamed:@"img_31.png"],[UIImage imageNamed:@"img_23.png"],@"教师交流",@"意见、反馈开通啦。"];
        
        _objects = [@[arr,arr1,arr2,arr3,arr4,arr5,arr6,arr7,arr8,arr9,arr10,arr11,arr12] mutableCopy];
    }
    return _objects;
}
static NSString *Identifier = @"Cell";

- (void)viewWillAppear:(BOOL)animated
{
    self.titleLabel.text = @"童米宝贝移动家园";
    
//    [self.funcButton setTitle:@"更多" andTarget:^(XRButton *sender) {
//        NSLog(@"123");
//    }];

    
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, applicationWidth, applicationHeight)];

    //主要内容
    mainScrollView.contentSize = CGSizeMake(applicationWidth, (applicationHeight + 100 + 57)*2 + 90);
    mainScrollView.delegate = self;
    mainScrollView.pagingEnabled = NO;
    mainScrollView.scrollEnabled = YES;
    mainScrollView.showsVerticalScrollIndicator = NO;
    mainScrollView.maximumZoomScale = 1.0;
    mainScrollView.minimumZoomScale = 1.0;
    [self.view addSubview:mainScrollView];
    
    
    //滚动图片广告
    scrollView = [[AdScrollView alloc]initWithFrame:CGRectMake(0, 0, applicationWidth, 300.0 / 720 * applicationWidth)];
    AdDataModel * dataModel = [AdDataModel adDataModelWithImageNameAndAdTitleArray];
    scrollView.imageNameArray = dataModel.imageNameArray;
    //点得位置
    scrollView.PageControlShowStyle = UIPageControlShowStyleRight;
    scrollView.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    scrollView.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    //文字
    [scrollView setAdTitleArray:dataModel.adTitleArray withShowStyle:AdTitleShowStyleLeft];
    //广告的点击事件
    UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    //    [singletap setNumberOfTapsRequired:1];
    //   singletap.delegate = self;
    [scrollView addGestureRecognizer:singletap];
    
    [mainScrollView addSubview:scrollView];
    
    
    //班级活动这一行
    UIView *functionView = [[UIView alloc]initWithFrame:CGRectMake(0, 300.0 / 720 * applicationWidth, applicationWidth, 95 * widthRate)];
    [functionView setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *oneButton = [[UIButton alloc] init];
    oneButton .frame = CGRectMake(8.5+applicationWidth/28,applicationWidth/28+1.5,applicationWidth/6,applicationWidth/6);
    [oneButton setImage:[UIImage imageNamed:@"icon_05.png"] forState:UIControlStateNormal];
    oneButton.backgroundColor = [UIColor clearColor];
    [oneButton addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:oneButton];
    
    UILabel *oneLabel = [[UILabel alloc]init];
    oneLabel.frame = CGRectMake(8.5+applicationWidth/28,applicationWidth/19+1.5+applicationWidth/6,applicationWidth/6,applicationWidth/28);
    oneLabel.text = @"中心动态";
    oneLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [functionView addSubview:oneLabel];
    
    UIButton *TwoButton = [[UIButton alloc] init];
    TwoButton .frame = CGRectMake(8.5+applicationWidth/28+applicationWidth/6+32/13*applicationWidth/28,applicationWidth/28+1.5,applicationWidth/6,applicationWidth/6);
    [TwoButton setImage:[UIImage imageNamed:@"icon_07.png"] forState:UIControlStateNormal];
    TwoButton.backgroundColor = [UIColor clearColor];
    [TwoButton addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:TwoButton];
    
    UILabel *TwoLabel = [[UILabel alloc]init];
    TwoLabel.frame = CGRectMake(8.5+applicationWidth/28+applicationWidth/6+32/13*applicationWidth/28,applicationWidth/19+applicationWidth/6+1.5,applicationWidth/6,applicationWidth/28);
    TwoLabel.text = @"班级管理";
    TwoLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [functionView addSubview:TwoLabel];
    
    UIButton *ThreeButton = [[UIButton alloc] init];
    ThreeButton .frame = CGRectMake(8.5+applicationWidth/28+(applicationWidth/6+32/13*applicationWidth/28)*2,applicationWidth/28+1.5,applicationWidth/6,applicationWidth/6);
    [ThreeButton setImage:[UIImage imageNamed:@"icon_09.png"] forState:UIControlStateNormal];
    ThreeButton.backgroundColor = [UIColor clearColor];
    [ThreeButton addTarget:self action:@selector(buttonClicked3:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:ThreeButton];
    
    UILabel *ThreeLabel = [[UILabel alloc]init];
    ThreeLabel.frame = CGRectMake(8.5+applicationWidth/28+(applicationWidth/6+32/13*applicationWidth/28)*2,applicationWidth/19+applicationWidth/6+1.5,applicationWidth/6,applicationWidth/28);
    ThreeLabel.text = @"家园互动";
    ThreeLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [functionView addSubview:ThreeLabel];
    
    UIButton *FourButton = [[UIButton alloc] init];
    FourButton .frame = CGRectMake(8.5+applicationWidth/28+(applicationWidth/6+32/13*applicationWidth/28)*3,applicationWidth/28+1.5,applicationWidth/6,applicationWidth/6);
    [FourButton setImage:[UIImage imageNamed:@"icon_11.png"] forState:UIControlStateNormal];
    FourButton.backgroundColor = [UIColor clearColor];
    [FourButton addTarget:self action:@selector(buttonClicked4:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:FourButton];
    
    UILabel *FourLabel = [[UILabel alloc]init];
    FourLabel.frame = CGRectMake(8.5+applicationWidth/28+(applicationWidth/6+32/13*applicationWidth/28)*3,applicationWidth/19+applicationWidth/6+1.5,applicationWidth/6,applicationWidth/28);
    FourLabel.text = @"教师助手";
    FourLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [functionView addSubview:FourLabel];
    [mainScrollView addSubview:functionView];
    
    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0 ,applicationWidth/1.4, applicationWidth, 2190 * widthRate) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    
    [mainScrollView addSubview:myTableView];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
    [myTableView addGestureRecognizer:longPress];
    myTableView.scrollEnabled = NO;
    [myTableView setSeparatorColor:[UIColor clearColor]];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];
    
    

    // Do any additional setup after loading the view.
}
-(void)handleSingleTap:(UIButton*)btn
{
    NSLog(@"我点广告了");
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.objects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"ss";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    
    if (cell == nil) {
        cell = [[MyCell alloc] initWithFrame:CGRectMake(0, 100, applicationWidth, 195 * widthRate)];
        
    }
    cell.myImageView7.image = self.objects[indexPath.row][0];
    cell.myImageView6.image = self.objects[indexPath.row][1];
    cell.myImageView.image = self.objects[indexPath.row][2];
    cell.myImageView1.image = self.objects[indexPath.row][3];
    cell.myImageView2.image = self.objects[indexPath.row][4];
    cell.myImageView3.image = self.objects[indexPath.row][5];
    cell.myImage.image = self.objects[indexPath.row][6];
    cell.myImageView5.image = self.objects[indexPath.row][7];
    cell.myLabel.text = self.objects[indexPath.row][8];
    cell.myLabel1.text = self.objects[indexPath.row][9];
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (95 * widthRate);
}

//中心动态
-(void)buttonClicked1:(UIButton*)btn
{
    DynamicViewController *dynamic = [[DynamicViewController alloc] init];
    
    [self.navigationController pushViewController:dynamic animated:NO];
    
    
    
}
-(void)buttonClicked2:(UIButton*)btn
{
    NSLog(@"班级管理");
}
-(void)buttonClicked3:(UIButton*)btn
{
    NSLog(@"家园互动");
}
-(void)buttonClicked4:(UIButton*)btn
{
    NSLog(@"教师助手");
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0 ) {
        NSLog(@"1");
    } else if (indexPath.row == 1) {
        NSLog(@"2");
    } else if (indexPath.row == 2) {
        NSLog(@"3");
    } else if (indexPath.row == 3) {
        NSLog(@"4");
    } else if (indexPath.row == 4) {
        NSLog(@"5");
    } else if (indexPath.row == 5) {
        NSLog(@"6");
    } else if (indexPath.row == 6) {
        NSLog(@"7");
    } else if (indexPath.row == 7) {
        NSLog(@"8");
    } else if (indexPath.row == 8) {
        NSLog(@"9");
    } else if (indexPath.row == 9) {
        NSLog(@"10");
    } else if (indexPath.row == 10) {
        NSLog(@"11");
    } else if (indexPath.row == 11) {
        NSLog(@"12");
    } else {
        NSLog(@"13");
    }
    
    
}



- (IBAction)longPressGestureRecognized:(id)sender {
    
    
    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;
    
    CGPoint location = [longPress locationInView:myTableView];
    NSIndexPath *indexPath = [myTableView indexPathForRowAtPoint:location];
    
    
    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.
    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.
    
    switch (state) {
        case UIGestureRecognizerStateBegan: {
            if (indexPath) {
                sourceIndexPath = indexPath;
                
                UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath];
                
                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];
                
                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;
                [myTableView addSubview:snapshot];
                
                [UIView animateWithDuration:0.25 animations:^{
                    
                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    cell.alpha = 0.0;
                    cell.hidden = YES;
                } completion:^(BOOL finished) {
                    //                    cell.hidden = NO;//问题？？？？？？？
                    
                }];
            }
            break;
        }
            
        case UIGestureRecognizerStateChanged: {
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;
            
            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
                
                
                // ... update data source.
                [self.objects exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
                
                // ... move the rows.
                [myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
                
                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            }
            break;
        }
            
        default: {
            // Clean up.
            UITableViewCell *cell = [myTableView cellForRowAtIndexPath:sourceIndexPath];
            cell.hidden = NO;
            cell.alpha = 3.0;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;
                
            } completion:^(BOOL finished) {
                
                sourceIndexPath = nil;
                [snapshot removeFromSuperview];
                snapshot = nil;
                
            }];
            
            break;
        }
    }
}

#pragma mark - Helper methods

/** @brief Returns a customized snapshot of a given view. */
- (UIView *)customSnapshoFromView:(UIView *)inputView {
    // Make an image from the input view.
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // Create an image view.
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 2.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    
    return snapshot;
}


@end
